package com.crm.services;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.jdao.IQaDAO;
import com.crm.model.mdao.DictionariesMapper;
import com.crm.model.mdao.QAMapper;
import com.crm.model.pojo.Complaint;
import com.crm.model.pojo.Qatable;
import com.crm.model.pojo.Timeline;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

/**
 * qa表业务类
 * @author 梁箐
 *
 */
@Service
public class QAServices {

	@Autowired
	QAMapper qaMapper;
	@Autowired
	IQaDAO qadao;
	@Autowired
	DictionariesMapper dictionariesMapper;
	
	/**
	 * 导出
	 * @return
	 */
	public List<Map<String, String>> selectAllInfo() {
	 return	qaMapper.selectAllInfo(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId());
	}
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectPage(int pageNum, int pageSize,Integer statueID,String question) {
		// 使用分页插件
		System.out.println("页码:" + pageNum + ",页数:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer classifyID = null;
		//对分类进行判断
		if (statueID!=null) {
			if (statueID==1387 || statueID==1388 || statueID==1389 || statueID==1390) {
				classifyID=statueID;
			}
		}
		System.out.println("编号："+statueID+"\t分类："+classifyID);
		// 获取记录数
		List<Map<String, String>> list = qaMapper.selectAllNew(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId(),classifyID,question);
		System.out.println("list :"+list);
	    return list;
	}
	
	/**
	 * 查询所有qa(用于客户投诉的解决)
	 * @return 
	 */
	public List<Map<String, String>> handleComplaint() {
		return qaMapper.selectAllTwo();
	}
	
	
	/**
	 * 根据编号查询单条
	 * @param qaid
	 * @return
	 */
	public Map<String, String> selectOneByid(Integer qaid) {
		return qaMapper.selectByid(qaid);
	}
	
	/**
	 * 根据编号查询点击次数
	 * @param qaid
	 * @return
	 */
	public Map<String, String> selectNum(Integer qaid) {
		return qaMapper.selectNum(qaid);
	}
	
	/**
	 * 查询QA字典表数据(根据父类编号)
	 * @return
	 */
	public List<Map<String, String>> selectQAtype(Integer pid) {
		return dictionariesMapper.selectByDicpar(pid);
	}

	/**
	 * 物理删除（更改状态）
	 * @return 
	 */
	public int deleteByStatue(Integer qaid) {
		return qaMapper.deleteByDeletestatue(qaid);
	}
	
	/**
	 * 查询所有作者
	 * @return
	 */
	public List<Map<String, String>> selectAllAuthor() {
		return qaMapper.selectAllAuthor();
	}
	
	/**
	 * 添加qa
	 */
	public Qatable addQA(Qatable qatable) {
		Qatable save = qadao.save(qatable);
		System.out.println(save);
		return save;
	}
	
	public int updateNum(Integer qaid) {
		return qaMapper.updateNum(qaid);
	}
	
}
